<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../layui/css/layui.css"/>
    <script src="../jQuery/jquery-1.11.2.js"></script>
    <script src="../layui/layui.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/font.css">
    <style>::-webkit-scrollbar{display: none;}</style>
</head>
<body>
<blockquote class="layui-elem-quote news_search">
    <table id="bg" lay-filter="bg"></table>
<script id="gjl" type="text/html">

<div class="layui-form-item">
    <form id="cxl" style="float: left" lay-filter="cxl" class="layui-form layui-form-item">
        <label class="layui-form-label" style="width: 120px">用户名:</label><input type="text" name="userName" placeholder="请输入用户名"  class="layui-input layui-input-inline">
         <label class="layui-form-label">状态:</label>
          <div class="layui-input-inline"><select  name="userState" id="userState">
            <option value="">---请选择---</option>
            <option value="0">未启用</option>
            <option value="1">启用</option>
        </select></div>
        <button class="layui-btn layui-btn-primary layui-bg-gray"   lay-submit id="cx" lay-filter="cx"><i class="layui-icon layui-icon-search"></i></button>
    </form>
    <!--头部工具栏-->
    <div class="layui-btn-container layui-input-inline" style="margin-left: 50px">
        <button class="layui-btn layui-btn-danger" lay-event="scs">
            <i class="layui-icon">&#xe640;</i>批量删除
        </button>
        <button class="layui-btn layui-btn-normal" lay-event="zj" style="margin-left: 130px;margin-top: -78px;">
            <i class="layui-icon">&#xe654;</i>添加用户
        </button>
    </div>
</div>
</script>
<div style="display: none;" id="dhk">
    <form class="layui-form" id="zjdhk" method="POST" lay-skin="line" lay-filter="dhk" style="margin-left: 100px;margin-top: 60px;">
        <div class="layui-form-item" id="bh" style="display: none">
            <label class="layui-form-label" >id：</label>
            <div class="layui-input-inline">
                <input type="text" name="userId"  placeholder="请输入用户编号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名：</label>
            <div class="layui-input-inline">
                <input type="text" name="userName" lay-verify="required" placeholder="请输入用户姓名" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="display: none">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="userPaw"  placeholder="请输入密码" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-inline">
                <input type="text" name="userPhone" lay-verify="required" placeholder="请输入电话" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div  class="layui-form-item" lay-filter="zw">
            <label class="layui-form-label" id="ss">角色</label>
            <div class="layui-input-inline" style="width: 180px">
                <select  name="roleId" id="roleId" >
                    <option value="">请选择</option>
                   <!-- <option value="1">管理员</option>
                    <option value="2">基金经理</option>
                    <option value="3">数据分析师</option>
                    <option value="4">会计</option>
                    <option value="5">TA管理员</option>-->
                </select>
            </div>
        </div>
        <div  class="layui-form-item" lay-filter="zt">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline" style="width: 180px">
                <select  name="userState" id="zt" >
                    <option value="">请选择</option>
                    <option value="1">启用</option>
                    <option value="2">未启用</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-bg-blue" id="tjs" lay-submit lay-filter="formSave">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
    <script id="gj" type="text/html">
        <button  class="layui-btn layui-btn-xs layui-btn-danger" lay-event="sc"><i class="layui-icon layui-icon-delete"></i>删除</button>
        <button  class="layui-btn layui-btn-xs layui-btn-normal" lay-event="xg"><i class="layui-icon layui-icon-edit"></i>修改</button>
        <button  class="layui-btn layui-btn-xs layui-btn-normal" lay-event="resetPwd"><i class="layui-icon layui-icon-edit"></i>重置密码</button>
    </script>
<script>
    layui.use(['table','layer','form'],function () {
     var table=layui.table;
        var layer=layui.layer;
        var form=layui.form;
        var shuj="";
        var kko="";


        table.render({
            elem: '#bg',//表格对象
            url:"/uc/us",//"json/emplayui.json",//实际使用servlet路径(后台服务路径)
            page: true,//分页
            method:'GET',//提交方式
            height:740,
            toolbar: '#gjl',//表格上方普通工具栏
            limit:15,//每页行数

            cols:[[
                {checkbox:true},
                {field:"userName",title:"用户名",sort:true,align:"center"},
                {field:"userPhone",title:"电话",sort:true,align:"center"},
                {field:"roleId",title:"角色",sort:true,align:"center",templet :function(d){
                    for (var i=0;i<shuj.length;i++){
                        if(d.roleId == shuj[i].roleId){
                            return '<span>'+shuj[i].roleName+'</span>';
                        }
                    }
                    }},
                {field:"userState",title:"状态",align:"center",templet :function(d){
                    //注意格式,此处d为对象，d.字段名来获得该字段值
                    if(d.userState == 1){
                        return '<span>启用</span>';
                    }else{
                        return '<span>未启用</span>';
                    }
                }},
                {fixed: 'right', title:'操作', toolbar: '#gj',width:250,align:"center"}
                ]],
            limits: [5, 10,20,30,50],
                parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                    shuj=res.xl;
                    for (var i=0;i<shuj.length;i++){
                        kko+="<option value='"+shuj[i].roleId+"'>"+shuj[i].roleName+"</option>";
                    }
                    $("#roleId").append(kko);
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": res.data //解析数据列表
                    }}
                });
        var ms="";
        form.on("submit(cxl)",function (msg) {
            table.reload("bg",{
                page:{
                    curr:1
                },
                where:{
                    userName:msg.field.userName,
                    userState:msg.field.userState
                }
            });
            return false;
        });

        form.on("submit(formSave)",function (msg) {
            if(msg.field.userPaw==""){
                msg.field.userPaw="88888888";
            }
           $.ajax({
                url:"/uc/us",
                method: ms,
                data:JSON.stringify(msg.field),
                   contentType:"application/json;charset=utf-8",
                success:function (sj) {
                    alert(sj)
                       if(sj=="cg"){
                           layer.closeAll();
                           location.reload();
                       }else{
                           alert("失败");
                       }
                }
            });
        });
        table.on("toolbar(bg)",function (msg) {
            var che=table.checkStatus(msg.config.id);

            if(msg.event=="scs"){
                var ss="";
                for (var i=0;i<che.data.length;i++){
                    ss+=che.data[i].userId+",";
                }
                $.ajax({
                    url:"/uc/uss/"+ss,
                    method: "put",
                    success:function (sj) {
                        if(sj=="cg"){
                            layer.closeAll();
                            location.reload();
                        }else{
                            alert("失败");
                        }
                    }

                });
            }else{
                //这是添加应该清空表单内容
                $("#zjdhk")[0].reset();
                layer.open({
                    title:"<span style='color:white' align='center'><i class='layui-icon'>&#xe61f;</i> 添加用户</span>",
                    type: 1,//外部页面为2，内部层1
                    area: ['40%', '65%'],
                    fix: false,
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    skin: 'layui-layer-lan',
                    content:$("#dhk").html()
                });
                ms="post";
                layui.use('form', function(){
                    var form = layui.form;
                    form.render(); //更新  所在容器内的全部 select 状态
                });

            }

        });
        table.on("tool(bg)",function (msg) {
            var data=msg.data;
            if(msg.event=="sc"){
                layer.confirm('你确定删除', function(index){
                    $.ajax({
                        url:"/uc/us/"+data.userId,
                        method: "delete",
                        success:function (sj) {
                            if(sj=="cg"){
                                layer.closeAll();
                                   msg.del();
                            }else{
                                alert("失败");
                            }
                        }

                    });

                });

            }else if (msg.event=="xg"){

                layer.open({
                title:"<span style=\'color:white\' align=\'center\'><i class=\'layui-icon\'>&#xe6b2;</i>修改用户信息</span>",
                type: 1,//外部页面为2，内部层1
                area: ['40%', '65%'],
                fix: false,
                maxmin: true,
                shadeClose: true,
                shade: 0.4,
                skin: 'layui-layer-lan',
                content:$("#dhk").html()
            });
            ms="put";

             form.val("dhk",{
                  userId:data.userId,
                 userName:data.userName,
                 userPaw:data.userPaw,
                 userPhone:data.userPhone,
                 roleId:data.roleId,
                 userState:data.userState
             });
            }else{
                //点击的是重置密码
                //绑定监听事件
                layer.confirm('您确认重置密码?', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url:"../uc/method/"+data.userId,
                        type:"post",
                        data:"",
                        success:function (d) {
                            layer.msg('重置成功', {icon: 1});
                        }
                    });
                }, function(){
                    layer.msg('你已取消', {
                        time: 2000, //20s后自动关闭
                    });
                });
            }
        });
    });
</script>
</blockquote>
</body>
</html>